/*
 * NIST Healthcare Test Framework
 * MockUpClient.java Jul 23, 2010
 *
 * This code was produced by the National Institute of Standards and
 * Technology (NIST). See the "nist.disclaimer" file given in the distribution
 * for information on the use and redistribution of this software.
 */
package gov.nist.healthcare.testframework.junit;

import gov.nist.healthcare.core.Constants.MessageEncoding;
import gov.nist.healthcare.core.message.HL7Message;
import gov.nist.healthcare.core.message.Name;
import gov.nist.healthcare.core.message.v2.er7.Er7Message;
import gov.nist.healthcare.core.message.v2.xml.XmlMessage;
import gov.nist.healthcare.core.message.v3.HL7V3Message;
import gov.nist.healthcare.testframework.TestFramework;
import gov.nist.healthcare.testframework.communication.CommunicationUtils;
import gov.nist.healthcare.testframework.communication.MllpUtils;
import gov.nist.healthcare.testframework.communication.SoapUtils;
import java.net.ServerSocket;
import java.net.Socket;
import javax.net.ServerSocketFactory;

/**
 * This is a mock up server for receiving message for testing.
 * 
 * @author Sydney Henrard (NIST)
 */
public class MockUpServer extends Thread {

    private int port;
    private boolean secure;
    private MessageEncoding encoding;

    /**
     * Start a server on a specific port
     * 
     * @param port
     * @param secure
     * @param encoding
     */
    public void startServer(int port, boolean secure, MessageEncoding encoding) {
        this.port = port;
        this.secure = secure;
        this.encoding = encoding;
    }

    @Override
    public void run() {
        try {
            ServerSocket server = null;
            if (!secure) {
                server = new ServerSocket(port);
            } else {
                ServerSocketFactory ssf = TestFramework.getInstance().getSSLContext().getServerSocketFactory();
                server = ssf.createServerSocket(port);
            }
            Socket connection = server.accept();
            String msg = null;
            HL7Message message = null;
            if (encoding == MessageEncoding.V2_ER7) {
                msg = MllpUtils.extractMessage(connection.getInputStream());
                message = new Er7Message(msg);
            } else if (encoding == MessageEncoding.V2_XML) {
                msg = MllpUtils.extractMessage(connection.getInputStream());
                message = new XmlMessage(msg);
            } else if (encoding == MessageEncoding.V3) {
                msg = SoapUtils.unWrapMessageFromHttpRequest(connection);
                message = new HL7V3Message(msg);
            }
            // Change the sender and receiver
            HL7Message messageToSend = null;
            Name sender = message.getSending();
            Name receiver = message.getReceiving();
            message.replaceSending(receiver);
            message.replaceReceiving(sender);
            messageToSend = message;
            if (encoding == MessageEncoding.V2_ER7) {
                CommunicationUtils.send(connection,
                        MllpUtils.wrapMessage(messageToSend));
            } else if (encoding == MessageEncoding.V2_XML) {
                CommunicationUtils.send(connection,
                        MllpUtils.wrapMessage(messageToSend));
            } else if (encoding == MessageEncoding.V3) {
                CommunicationUtils.send(connection,
                        SoapUtils.wrapMessageInHttpResponse(messageToSend));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
